package com.cloud.template.base.util;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * Title: 资源文件读取工具类
 * 
 */

public class PropertyUtil {
	private static final Logger LOGGER= LogManager.getLogger(PropertyUtil.class);
	
	private static Map instance = Collections.synchronizedMap(new HashMap());
	private static Object lock = new Object();
	protected String sourceUrl;
	protected ResourceBundle resourceBundle;
	private static Map<String, String> convert = Collections
			.synchronizedMap(new HashMap<String, String>());

	protected PropertyUtil(String sourceUrl) {
		this.sourceUrl = sourceUrl;
		load();
	}

	public static PropertyUtil getInstance(String sourceUrl) {
		synchronized (lock) {
			PropertyUtil manager = (PropertyUtil) instance.get(sourceUrl);
			if (manager == null) {
				manager = new PropertyUtil(sourceUrl);
				instance.put(sourceUrl, manager);
			}
			return manager;
		}
	}

	private synchronized void load() {
		try {
			resourceBundle = ResourceBundle.getBundle(sourceUrl);
		} catch (Exception e) {
			throw new RuntimeException("sourceUrl = " + sourceUrl
					+ " file load error!", e);
		}
	}

	public synchronized String getProperty(String key) {
		try {
			return resourceBundle.getString(key);
		} catch (Exception e) {
			LOGGER.error("获取值出错", e);
		}
		return null;
	}

}
